package com.tdam2011.grupo05.control.connectivity;

import com.tdam2011.grupo05.control.notification.NotificationNetwork;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class ConnectivityListener {

	private static final String TAG = "ConnectivityListiner";

	private final Context mContext;

	private boolean active;

	private boolean isFailover;

	private NetworkInfo mNetworkInfo;

	private NetworkInfo mWifi;

	private NetworkInfo mMobile;

	private String mReason;

	private long timestamp;

	private boolean stateConnection = true;

	private final ConnectivityBroadcast mBroadcast;

	private final ConnectivityManager mConnectivityManager;

	private static final IntentFilter CONNECTIVITY_ACTION = new IntentFilter(
			ConnectivityManager.CONNECTIVITY_ACTION);

	private ConnectionListener mListener;

	public interface ConnectionListener {

		void connectionDead();

		void connectionAlive();
	}

	public ConnectivityListener(Context context) {
		Log.d(TAG, "create connectivity");
		mContext = context;
		active = false;
		mBroadcast = new ConnectivityBroadcast();
		mConnectivityManager = (ConnectivityManager) mContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
	}

	public boolean isActive() {
		return active;
	}

	public synchronized void registerReceiver() {
		Log.d(TAG, "start connectivity");
		mContext.registerReceiver(mBroadcast, CONNECTIVITY_ACTION);
		active = true;
	}

	public synchronized void unregisterReceiver() {
		if (active) {
			Log.d(TAG, "stop connectivity");
			mContext.unregisterReceiver(mBroadcast);
		}
	}

	public void setListener(ConnectionListener l) {
		mListener = l;
	}

	public void removeListener() {
		mListener = null;
	}

	public boolean isMobileConnected() {
		mMobile = mConnectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (mMobile != null) {
			return mMobile.isConnected();
		}
		return false;
	}

	public boolean isWifiConnected() {
		mWifi = mConnectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (mWifi != null) {
			return mWifi.isConnected();
		}
		return false;
	}

	private class ConnectivityBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();

			if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
				return;
			}
			Log.d(TAG, "Connectivity Broadcast");

			boolean noConnection = intent.getBooleanExtra(
					ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

			mNetworkInfo = intent
					.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

			if (noConnection) {
				if (mListener != null) {
					mListener.connectionDead();
				}
				if (stateConnection) {
					NotificationNetwork.notifyDisconnected(mContext,
							"Sin Conexion");

					stateConnection = false;
				}
			} else {
				if (mListener != null) {
					mListener.connectionAlive();
				}
				if (!stateConnection) {
					if (mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
						NotificationNetwork.notifyMobileConnected(mContext,
								"Mobile");
					} else if (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
						NotificationNetwork.notifyMobileConnected(mContext,
								"Wifi");
					}
					stateConnection = true;
				}
			}

			mReason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);

			isFailover = intent.getBooleanExtra(
					ConnectivityManager.EXTRA_IS_FAILOVER, false);

			timestamp = System.currentTimeMillis();

			Log.d(TAG, "connectivity networkinfo=" + mNetworkInfo.getTypeName()

			+ " reason=" + mReason + " is failover=" + isFailover
					+ " timestamp=" + timestamp);

		}
	}

}
